<template>
  <div>
    <div class="search">
      <input
        class="input"
        type="text"
        placeholder="搜索你想要的的商品"
        placeholder-style="color:#333"
        v-model="filterVal"
        confirm-type="search"
        @confirm="handleSearch"
      />
      <icon
        @click="handleSearch"
        class="searchClick"
        type="search"
        size="20"
        color="#333"
      />
    </div>
    <div class="list">
      <p class="listHint">提示：加入市场，只能选择已上架商品</p>
      <scroll-view
        class="scroll-wrap no-scroll-bar"
        style="height:calc(100% - 20px)"
        scroll-y
        @scrolltolower="handleLoadMore"
      >
        <div v-for="(item,idx) in list" :key="idx" class="goods-block">
          <img :src="item.goodsImg" mode="aspectFit" class="preview" />
          <div class="goods-info">
            <div class="top">
              <p class="goods-name ellipsis">{{item.goodsName}}</p>
              <div class="goodsMo">
                <span class="discount">{{item.goodsDiscount}}</span>
                <p class="goods-stock">库存: {{item.goodsStock}}</p>
              </div>
            </div>
            <div class="bottom">
              <span class="discount">佣金：￥{{item.disAmount}}</span>
            </div>
          </div>
          <div class="edit-block">
            <button
              class="edit-btn"
              size="mini"
              @click="handleEditGoods(item)"
            >加入市场</button>
          </div>
        </div>
        <p v-if="alreadyLastPage" class="end-tip">没有更多了</p>
      </scroll-view>
    </div>
    <div class="alertBox" v-if="isAlert">
      <div class="cont">
        <div class="title">
          <span>请输入分销佣金。</span>
          <input
            type="digit"
            placeholder="请输入分销佣金。"
            v-model="goodForm.disAmount"
            class="amountInput"
          />
        </div>
        <div class="specBox">
          <div v-for="(item,index) in specList" :key="index">
            <span>{{item.goodsSpecName}}</span>
            <input type="digit" placeholder="请输入分销佣金" v-model="item.disAmount" />
          </div>
        </div>

        <button class="btn" @click="handleAffirm">确认</button>
        <div class="close" @click="closeAlert">
          <icon type="clear" size="28" color="#fff" />
        </div>
      </div>

      <div class="zz"></div>
    </div>
  </div>
</template>

<script>
import {} from '@/utils'
import {
  FIND_SHOP_GOODS_PAGE,
  UPDATE_SHOP_GOODS_INFO,
  FIND_SHOP_GOODS_SPECIFICATION_LIST,
  UPDATE_SHOP_GOODS_SPECIFICATION
} from '@/api'

export default {
  components: {},
  props: {},
  data () {
    return {
      isAlert: false,
      filterVal: '',
      goodsList: [],
      currentPage: 1,
      alreadyLastPage: false,
      goodForm: {},
      specList: []
    }
  },
  computed: {
    list () {
      return this.goodsList.map(_ => ({
        goodsAmount: '￥' + (_.goodsAmount * 1).toFixed(2),
        disAmount: (_.disAmount * 1).toFixed(2),
        goodsDiscount: '￥' + (_.goodsDiscount * 1).toFixed(2),
        goodsName: _.goodsName,
        goodsNo: _.goodsNo,
        goodsStock: _.goodsStock,
        goodsImg: _.goodsImg,
        goodsKind: _.goodsKind,
        goodsCost: '￥' + (_.goodsCost * 1).toFixed(2)
      }))
    },
    account () {
      return this.$cookie.get('account')
    }
  },
  watch: {},
  created () {},
  mounted () {},
  onReady () {},
  onLoad () {},
  onShow () {
    this.$cookie.remove('describe-img1')
    this.$cookie.remove('describe-img2')
    this.goodsList = []
    this.currentPage = 1
    this.getGoodsList()
  },
  methods: {
    handleSearch () {
      this.currentPage = 1
      this.getGoodsList()
    },
    handleLoadMore () {
      if (!this.alreadyLastPage) {
        this.getGoodsList()
      }
    },
    handleEditGoods (item) {
      const vm = this
      vm.isAlert = true
      vm.goodForm = item
      vm.findGoodsSize()
    },
    closeAlert () {
      const vm = this
      vm.isAlert = false
      vm.goodForm = {}
      vm.specList = []
    },
    handleAffirm () {
      const vm = this
      vm.updateDisShop()
      if (vm.specList.length) {
        vm.specList.forEach(_ => {
          if (!_.disAmount) {
            _.disAmount = 0
          }
          vm.updateSpec(_)
        })
      }
    },
    getGoodsList () {
      const vm = this
      let params = {
        shopId: vm.account.shopId,
        isShelf: '1',
        isDis: 'N'
      }
      if (vm.filterVal) {
        if (/^\d*$/.test(vm.filterVal) && vm.filterVal.length === 18) {
          params.goodsNo = vm.filterVal
        } else {
          params.goodsName = vm.filterVal
        }
      }
      FIND_SHOP_GOODS_PAGE(params, vm.currentPage, 10)
        .then(r => {
          if (r.data.code === '0') {
            let { list, isFirstPage, isLastPage } = r.data.data
            vm.printList(list, isFirstPage, isLastPage)
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    },
    printList (list, isFirst, isLast) {
      if (isFirst) {
        this.goodsList = list
        if (isLast || !list.length) {
          // only one page
          this.alreadyLastPage = true
        } else {
          // has next page
          this.alreadyLastPage = false
          this.currentPage += 1
        }
      } else if (isLast) {
        // more than one page and this is the last page
        this.goodsList.push(...list)
        this.alreadyLastPage = true
      } else {
        // middle page
        this.goodsList.push(...list)
        this.alreadyLastPage = false
        this.currentPage += 1
      }
    },
    updateDisShop () {
      const vm = this
      let params = {}
      params.goodsNo = vm.goodForm.goodsNo
      params.disAmount = vm.goodForm.disAmount
      params.shopId = vm.account.shopId
      params.isDis = 'Y'
      UPDATE_SHOP_GOODS_INFO(params)
        .then(r => {
          if (r.data.code === '0') {
            vm.goodsList = []
            vm.currentPage = 1
            vm.closeAlert()
            vm.getGoodsList()
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }, // 命名错了，不想改了，管他的
    updateSpec (item) {
      const vm = this
      UPDATE_SHOP_GOODS_SPECIFICATION(item)
        .then(r => {
          if (r.data.code === '0') {
            // do nothing
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }, // 更新规格
    findGoodsSize () {
      const vm = this
      FIND_SHOP_GOODS_SPECIFICATION_LIST(vm.goodForm.goodsNo)
        .then(r => {
          if (r.data.code === '0') {
            let list = r.data.data
            vm.specList = list
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.error(e)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
@import 'static/styles/color.scss';

.search {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 70px;
  position: relative;
  .input {
    width: 200px;
    height: 30px;
    padding-left: 55px;
    font-size: 14px;
    color: #fff;
    border: 1px solid #cccccc;
    border-radius: 25px;
  }
  .searchClick {
    position: absolute;
    left: 80px;
    top: 50%;
    transform: translate(0, -50%);
    z-index: 5;
  }
}
.list {
  height: calc(100% - 75px);
  .listHint {
    width: calc(100% - 20px);
    margin: auto;
    font-size: 13px;
    color: #d9412d;
  }
  > .scroll-wrap {
    .goods-block {
      width: calc(100% - 20px);
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 15px 0;
      margin: 0 10px 10px;
      // box-shadow: 0 0 8px lighten($blue, 20%);
      height: 80px;
      position: relative;
      overflow: hidden;
      transform: translateY(10px);
      > .preview {
        width: 70px;
        height: 70px;
      }
      > .goods-info {
        position: relative;
        font-size: 14px;
        width: calc(100% - 150px);
        height: 70px;
        .top {
          position: absolute;
          top: 0;
          font-size: 13px;
          > .goodsMo {
            display: flex;
            .goods-stock {
              margin-left: 15px;
              color: #767676;
            }
          }
        }
        .bottom {
          position: absolute;
          bottom: 0;
          > .discount {
            display: inline-block;
            color: $red;
            min-width: 50px;
            padding-right: 10px;
          }
        }
      }
      > .edit-block {
        display: flex;
        align-items: center;
        height: 70px;
        > .edit-btn {
          padding: 0 10px;
          border-radius: 25px;
          display: block;
          color: $blue;
          border: 1px solid $blue;
          font-size: 10px;
          background-color: #fff;
          height: 20px;
          line-height: 20px;
          transform: translateY(5px);
        }
      }
    }
    .end-tip {
      margin: 0;
      padding: 15px;
      text-align: center;
      color: $gray;
      font-size: 15px;
    }
  }
}
.alertBox {
  .cont {
    padding: 20px;
    border: 2px solid #eee;
    border-radius: 6px;
    background: #fff;
    position: fixed;
    z-index: 21;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
      font-size: 18px;
      color: #000000;
      text-align: center;
      span {
        margin: 8px 0;
      }
      .amountInput {
        margin: 16px 0;
        border-bottom: 2px solid #000;
      }
    }
    .specBox {
      font-size: 14px;
      color: #000000;
      div {
        width: 220px;
        margin: 14px auto;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
    .btn {
      width: 280px;
      height: 40px;
      line-height: 40px;
      border-radius: 25px;
      margin: auto;
      color: #fff;
      background: linear-gradient(to right, $new-dark-blue, $new-light-blue);
    }
    .close {
      position: absolute;
      z-index: 22;
      left: 50%;
      bottom: -50%;
      transform: translate(-50%, 0%);
    }
  }
  .zz {
    position: fixed;
    z-index: 20;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-color: #000000;
    opacity: 0.3;
  }
}
</style>
